using System;
using System.Collections.Generic;
using System.Text;

using libsecondlife;
using libsecondlife.InventorySystem;

using Feathertail;

namespace NestBox.Inventory
{
    class mv : Feathertail.Command
    {
        private FeathertailClient _Client;

        #region Command Members

        public void Setup(FeathertailClient Client)
        {
            _Client = Client;

            if (!_Client.FeathertailState.ContainsKey(Helpers.CurrentDirectoryKey))
            {
                _Client.FeathertailState[Helpers.CurrentDirectoryKey] = "/";
            }
        }

        public string HandleInput(string[] CmdLine)
        {
            if (CmdLine.Length < 3)
            {
                return GetHelpText();
            }

            StringBuilder sb = new StringBuilder();

            string curDirectory = (string)_Client.FeathertailState[Helpers.CurrentDirectoryKey];
            string targetDir = "";

            string itemString = CmdLine[1];
            string dirString = CmdLine[2];

            if (dirString.Equals(".."))
            {
                targetDir = curDirectory.Substring(0, curDirectory.LastIndexOf("/"));
            }
            else if (dirString.StartsWith("/"))
            {
                targetDir += dirString;
            }
            else
            {
                if (!curDirectory.Equals("/"))
                {
                    targetDir = curDirectory + "/";

                }

                targetDir += dirString;
            }

            InventoryFolder targetInvFolder = _Client.Inventory.getFolder(targetDir);
            if (targetInvFolder == null)
            {
                sb.AppendLine("Can't find target folder: " + targetDir);
                return sb.ToString();
            }
            else
            {
                sb.AppendLine("Moving file(s) to: " + targetDir);
            }

            InventoryFolder curInvFolder = _Client.Inventory.getFolder(curDirectory);
            if (curInvFolder.RequestDownloadContents(false, true, true).RequestComplete.WaitOne(15000, false) == false)
            {
                sb.AppendLine("Timed out refreshing contents of : " + curInvFolder.Name);
            }

            List<InventoryItem> stuff2move = new List<InventoryItem>();
            foreach (InventoryBase ib in curInvFolder.GetContents())
            {
                if (ib is InventoryItem)
                {
                    InventoryItem item = (InventoryItem)ib;
                    if (item.ItemID.Equals(itemString) || item.Name.Equals(itemString))
                    {
                        stuff2move.Add(item);
                    }
                }
            }

            // Actually move stuff, done here so above enumerator doesn't explode
            foreach( InventoryItem item in stuff2move )
            {
                sb.Append("Moving " + item.Name + " [" + item.ItemID + "] ... ");
                item.MoveTo(targetInvFolder);
                sb.AppendLine("Done");
            }

            return sb.ToString();
        }

        public string[] GetCommandStrings()
        {
            return new string[] { "mv", "move" };
        }

        public string GetSummaryText()
        {
            return "Move an item to another folder";
        }

        public string GetHelpText()
        {
            return "Usage: mv [item] [directory]" + Environment.NewLine + "Example: mv Beer /FridgeFolder";
        }

        #endregion
    }
}
